import Mock from 'mockjs'
import tableTypes from './tableTypes'
import { baseUrl } from '@/config/env'
const Random = Mock.Random

const createMockOption = option => {
    const mockOption = {}
    mockOption.id = Mock.mock('@increment')
    Object.keys(option).forEach(key => {
        if (option[key] === 'image') {
            mockOption[key] = Random.image('200x100', '#4A7BF7', 'Hello')
        } else if (option[key] === 'price') {
            mockOption[key] = Mock.mock('@float(0, 99999, 2, 2)')
        } else if (option[key] === 'is') {
            mockOption[key] = Mock.mock('@integer(1, 2)')
        } else if (option[key] === 'citys') {
            mockOption[key] = Object.keys(Mock.mock({
                "object|1-4": {"广州":true, "上海":true, "长沙":true, "四川":true, "宁波":true, "深圳":true}
            }).object)
        } else if (option[key] === 'category') {
            mockOption[key] = Object.keys(Mock.mock({
                "object|1-4": {"桌子":true, "椅子":true, "沙发":true, "冰箱":true, "茶几":true, "宠物":true}
            }).object)
        } else if (typeof option[key] === 'object' && option[key].regexp) {
            mockOption[key] = Mock.mock(option[key]).regexp
        } else {
            mockOption[key] = Mock.mock(option[key])
        }
    })
    return mockOption
}

const createTableData = (body, opt) => {
    body = typeof body === 'string' ? JSON.parse(body) : body
    let list = []
    for (let i = 0; i < body.pageSize; i++) {
        list.push(createMockOption(opt))
    }
    return {
        count: 1003,
        pageSize: body.pageSize,
        bussData: list
    }
}

export default ({ mock }) => {
  if (!mock) return;
  tableTypes.forEach(table => {
      let type = table.type || 'post'
      Mock.mock(baseUrl + table.url, type, opt => {
          return createTableData(opt.body, table.data)
      })
  })
}
